package com.cyc.basic.test.suanfa;

/**
 * @author: chenyunchang
 * @date 2023/8/25 18:50
 * @description:
 */
public class 二分查找 {

    public static void main(String[] args) {

    }

    public int searchTarget(int[] nums, int target){
        int left = 0;
        int right = nums.length - 1;
        int index = 0;
        while(left <= right){
            int mid = left + (right - left) / 2;
            if(nums[mid] > target){
                right = mid -1;
            } else if (nums[mid] < target){
                left = mid + 1;
            }else if(nums[mid] == target){
                index = mid;
                break;
            }
        }
        return index;
    }
}
